#!/usr/bin/python3

from PIL import Image, ImageDraw, ImageFont, ImageSequence
import time
import random
import led_driver
from threading import Timer

STEPS = 20
STEPS_RATE = 1.0 / STEPS

led_driver.Open(5000000)

led_driver.SetGlobalBright(2)

image = Image.new('RGB', (32, 16))
draw = ImageDraw.Draw(image)

class BlinkStar():
	def __init__(self):
		self.x = random.randrange(0, 32)
		self.y = random.randrange(0, 16)
		self.c_r = random.randrange(0, 256)
		self.c_g = random.randrange(0, 256)
		self.c_b = random.randrange(0, 256)
		self.step = 0 - random.randrange(0, STEPS)
		self.dir = 0

	def draw(self, led_draw):
		if (self.step >= 0):
			self._c_r = int(self.c_r * (self.step * STEPS_RATE))
			self._c_g = int(self.c_g * (self.step * STEPS_RATE))
			self._c_b = int(self.c_b * (self.step * STEPS_RATE))
			led_draw.point([(self.x, self.y)], fill = (self._c_r, self._c_g, self._c_b))
			if (self.step == 0):
				self.dir = 1	
			elif (self.step == STEPS):
				self.dir = -1

			self.step += self.dir
		else:
			self.step += 1

image = Image.new('RGB', (32, 16))
draw = ImageDraw.Draw(image)

while 1:
	draw.rectangle([0, 0, 31, 15], 0)
	# 随机星点、颜色、10个以内
	count = random.randrange(1, 24)
	stars = []
	# count = 1
	for i in range(count + 1):
		s = BlinkStar()
		stars.append(s)

	led_driver.Display(image.tobytes())

	round = random.randrange(6, 10)

	for i in range(STEPS * round):
		for s in stars:
			s.draw(draw)
		led_driver.Display(image.tobytes())
		time.sleep(0.05)






